package com.shop.admin.controller;

import com.shop.admin.entity.ShopCategory;
import com.shop.admin.entity.TakeoutMerchantCategory;
import com.shop.admin.result.ApiResult;
import com.shop.admin.service.TakeoutMerchantCategoryService;
import com.shop.admin.utils.PageUtils;
import com.shop.admin.valid.AddGroup;
import com.shop.admin.valid.UpdateGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

@Api(tags = {"TakeoutMerchantCategoryController"},description = "外卖商家分类接口")
@RestController
@RequestMapping("/api/takeout/merchant/category")
public class TakeoutMerchantCategoryController {

    @Resource
    private TakeoutMerchantCategoryService takeoutMerchantCategoryService;
    /**
     * 分类列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "get分类列表",notes = "")
    public ApiResult list(@ApiParam("参数为：id,merchantId,merchantCategoryId,categoryName")@RequestParam Map<String, Object> params){
        PageUtils page = takeoutMerchantCategoryService.queryPage(params);
        return ApiResult.ok(page);
    }

    //新增分类
    @PostMapping("/save")
    @ApiOperation(value = "新增分类",notes = "分类名字,url,排序")
    public ApiResult save(@Validated(AddGroup.class) @ApiParam("参数为：cateName,cateUrl,sort") @RequestBody TakeoutMerchantCategory category){
        if (takeoutMerchantCategoryService.selectBoolean(category.getCateName())){
            category.setCateName(category.getCateName());
            category.setCateUrl(category.getCateUrl());
            category.setSort(category.getSort());
            category.setCreateTime(new Date());
            category.setUpdateTime(new Date());
            takeoutMerchantCategoryService.save(category);
            return ApiResult.ok("新增成功！");
        }
        return ApiResult.ok("分类重复");
    }

    //修改分类
    @PostMapping("/update")
    @ApiOperation(value = "分类修改",notes = "分类id,分类名字,cateUrl,排序")
    public ApiResult updateSort(@Validated(UpdateGroup.class) @ApiParam("参数为：cateId,cateName,cateUrl,sort") @RequestBody TakeoutMerchantCategory category){
        if (takeoutMerchantCategoryService.selectBoolean(category.getCateName())){
            takeoutMerchantCategoryService.updateCascade(category);
            return ApiResult.ok("修改成功！");
        }
        return ApiResult.ok("分类重复");
    }

    //删除分类
    @PostMapping("/delete")
    @ApiOperation(value = "分类删除",notes = "分类id,id可为数组类型")
    public ApiResult updateSort(@ApiParam("参数为：cateId[]") @RequestBody Long[] ids){
        takeoutMerchantCategoryService.removeCategoryByIds(Arrays.asList(ids));
        return ApiResult.ok("删除成功！");
    }
}
